<template>
  <div>
    <!-- 经销商名称\代码搜索 -->
    <TopFilter
      :list="topFilterInit"
      :cols="2"
      :collapse="collapse"
      labelWidth="90"
      @filterChange="changeHandle"
    ></TopFilter>
    <!-- table -->
    <FilterTable
      height="250"
      ref="listModal"
      :columns="columns"
      :selectionType="selectType"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :onColumnsChange="columns => this.columns = columns"
      :onRowSelectChange="selectChange"
      columnsRef="cms-common_dealerdata_dealerType_01"
    ></FilterTable>
    <div class="dialogBottmView">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button @click="saveDialog" type="primary">确定</el-button>
    </div>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { getDealerList, pageDealerInfoList, getRegion, getCommunity } from '@/api/cms-common/dealerdata';
import { DICT_CODE } from '@/api/cms-api/origin/commonDealerType'; //经销商类型工具类
import { region } from '@/api/cms-common/region'; //行政区域

export default {
  mixins: [dictionary, region],
  props: {
    value: {
      type: Object,
      required: true
    },
    deptId: {
      type: Number
    },
    dealerType: {
      type: String
    },
    selectType: {
      type: String,
      default: 'multiple'
    }
  },
  data() {
    return {
      excludeBigCustomer: this.value.excludeBigCustomer ? 25381006 : null,
      visible: false,
      visibleDrawer: false,
      collapse: false,
      infoId: null,
      topFilterInit: this.topFilterInitFun(),
      flterValue: {}, //参数
      columns: this.creatHeader(),
      fetchApi: pageDealerInfoList,
      fetchParams: {
        excludeBigCustomer: this.value.excludeBigCustomer ? 25381006 : null,
        isDeleted: 0, //未删除
        status: 10031001, //启用
        auditStatus: 25511005, //已审核
        deptId: this.deptId,
        // dealerTypes: '25381001,25381002,25381003,25381004,25381005'
        dealerType: this.dealerType
      }, //ipa 接口
      selectedRows: [] //选中经销商
    };
  },
  methods: {
    //搜索数据
    topFilterInitFun() {
      return [
        { type: 'SELECT', label: '事业部', fieldName: 'deptId', placeholder: '请选择', initialValue: '', itemList: [] },
        { type: 'INPUT', label: '经销商代码', fieldName: 'dealerCode', placeholder: '请输入', initialValue: '' },
        { type: 'INPUT', label: '经销商简称', fieldName: 'dealerShortname', placeholder: '请输入', initialValue: '' },
        { type: 'SELECT', label: '省份', fieldName: 'provinceId', placeholder: '请选择', initialValue: '', itemList: [], change: props => this.initCity(props) },
        { type: 'SELECT', label: '城市', fieldName: 'cityId', placeholder: '请选择', initialValue: '', itemList: [] },
        { type: 'SELECT', label: '办事处', fieldName: 'officeId', placeholder: '请选择', initialValue: '', itemList: [] }
      ];
    },
    //搜索获取数据
    changeHandle(val) {
      let extP = {
        
        excludeBigCustomer: this.value.excludeBigCustomer ? 25381006 : null,
        isDeleted: 0, //未删除
        status: 10031001, //启用
        auditStatus: 25511005, //已审核
        deptId: this.deptId != null ? this.deptId : val.deptId,
        dealerType: this.dealerType
      };
      this.fetchParams = Object.assign({}, val, extP);
    },
    //table表头和数据源
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        { title: '公司全称', dataIndex: 'dealerName', sorter: true },
        { title: '经销商代码', dataIndex: 'dealerCode', sorter: true },
        { title: '经销商简称', dataIndex: 'dealerShortname', sorter: true },
        {
          title: '经销商类型',
          dataIndex: 'dealerType',
          sorter: true,
          render: props => {
            return <span>{this.createDictText(props.row.dealerType, DICT_CODE)}</span>;
          }
        },
        {
          title: '经销商级别',
          dataIndex: 'shopLevel',
          sorter: true,
          render: props => {
            return <span>{this.createDictText(props.row.shopLevel, '2540')}</span>;
          }
        },
        { title: '事业部', dataIndex: 'deptName', sorter: true, width: 200 },
        { title: '办事处', dataIndex: 'officeName', sorter: true },
        // { title: '省份', dataIndex: 'provinceId', sorter: true , render: props => {
        //     return <span>{this.createDictText(props.row.provinceId, '1056')}</span>;
        //   }},
        // { title: '城市', dataIndex: 'cityId', sorter: true ,render: props => {
        //     return <span>{this.createDictText(props.row.cityId, '1056')}</span>;
        //   }},
        // { title: '县区', dataIndex: 'countyId', sorter: true ,render: props => {
        //     return <span>{this.createDictText(props.row.countyId, '1056')}</span>;
        //   }}
        {
          title: '省份',
          dataIndex: 'provinceId',
          sorter: true,
          render: props => {
            return <span>{this.createRegionText(props.row.provinceId)}</span>;
          }
        },
        {
          title: '城市',
          dataIndex: 'cityId',
          sorter: true,
          render: props => {
            return <span>{this.createRegionText(props.row.cityId)}</span>;
          }
        },
        {
          title: '县区',
          dataIndex: 'districtId',
          sorter: true,
          render: props => {
            return <span>{this.createRegionText(props.row.districtId)}</span>;
          }
        }
      ];
    },
    //选中
    selectChange(rows) {
      this.selectedRows = rows;
      console.log(this.selectedRows);
    },
    //保存
    saveDialog() {
      console.log('excludeBigCustomer:' + this.excludeBigCustomer);
      if (this.selectedRows.length == 0) {
        this.$notify.warning({ title: '提示信息', message: '请勾选经销商!' });
        return;
      }
      this.$emit('close', { visible: false, data: this.selectedRows });
    },
    //取消操作
    closeDialog() {
      // this.$emit('close', { visible: false ,"data":this.selectedRows});
      this.$emit('close', { visible: false });
    },
    //加载事业部数据
    async getDeptData() {
      const res = await getRegion({});
      if (res.resultCode === 200) {
        this.topFilterInit.find(item => item.fieldName === 'deptId').itemList = res.data.map(x => ({ value: x.id, text: x.orgName }));
      }
    },
    //初始化所在省份
    async initProvince() {
      let pl = await this.createRegionList();
      this.topFilterInit.find(item => item.fieldName === 'provinceId').itemList = pl;
    },
    //初始化所在市数据
    async initCity(provinceId) {
      if (!provinceId || provinceId == null || typeof provinceId == 'undefined') {
        return;
      }
      //取省数据
      let rd = this.getRegionData();
      this.cityData = rd[provinceId].children;
      let cl = this.createSubRegionList(this.cityData);
      //初始化市，清除原值
      let city = this.topFilterInit.find(item => item.fieldName === 'cityId');
      city.itemList = cl;
      city.initialValue = '';
      //清除区县
      let district = this.topFilterInit.find(item => item.fieldName === 'districtId');
      district.itemList = [];
      district.initialValue = '';
    },
    //获取办事处下拉列表数据
    async getOfficeData() {
      const res = await getCommunity(this.deptId);
      if (res.resultCode === 200) {
        this.topFilterInit.find(item => item.fieldName === 'officeId').itemList = res.data.map(x => ({ value: parseInt(x.id), text: x.orgName }));
      }
    }
  },
  mounted() {
    //this.BaseTable = this.$refs.listModal.$refs.pageTable;
    this.getDeptData();
    //初始化省份信息
    this.initProvince();
    //初始化办事处
    this.getOfficeData();
  }
};
</script>

<style lang="less" scoped>
</style>
